package com.example.logining.expression;

import com.example.logining.domain.vo.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @title: ExpressionRoot
 * @author: wywang
 * @date: 2024/11/17
 * @description: 自定义权限校验方法
 **/
@Component("exRoot")
public class ExpressionRoot {
    public boolean hasAuthority(String authority) {
//获取当前用户的权限
        Authentication authentication =
                SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        List<String> permissions = loginUser.getPermissions();
//判断用户权限集合中是否存在authority
        return permissions.contains(authority);
    }
}